#include "qdrivermessagetextedit.h"

#include <QPainter>
#include <QFontMetrics>
#include <QTextBlock>
#include <QContextMenuEvent>
#include <QMenu>
#include <QFileDialog>
#include <QFile>

#include "../../libs/shared/xmlobject.h"

QDriverMessageTextEdit::QDriverMessageTextEdit(QWidget *parent) :
    QPlainTextEdit(parent)
{
    this->setWordWrapMode(QTextOption::NoWrap);
}

void QDriverMessageTextEdit::paintEvent(QPaintEvent *e)
{
    QPainter p(viewport());

    QFontMetrics fm=fontMetrics();

    int h=fm.height();

    QRect re=viewport()->rect();

    QColor c1=QColor(255,230,191);
    QColor c2=c1.lighter(112);
    QString str;
    QTextBlock block=firstVisibleBlock();
    qreal top=0;
    int w1=fm.width("WRITE ");
    int w2=fm.width("WRITE  2000-00-00 00:00:00.000 ");
    p.setPen(QPen(QColor(0,0,0)));
    while(block.isVisible() && top<re.bottom())
    {
        if(block.isValid())
        {
            str=block.text();
            if(block.text().startsWith("WRITE"))
            {
                p.fillRect(QRect(0,top,re.width(),h),c1);
                p.drawLine(w1,top,w1,top+h);
                p.drawLine(w2,top,w2,top+h);
            }
            else if(block.text().startsWith("READ"))
            {
                p.fillRect(QRect(0,top,re.width(),h),c2);
                p.drawLine(w1,top,w1,top+h);
                p.drawLine(w2,top,w2,top+h);
            }
            else
            {
                p.fillRect(QRect(0,top,re.width(),h),c1.lighter(120));
            }
        }
        else
        {
            p.fillRect(QRect(0,top,re.width(),h),c1.lighter(120));
        }
        top+=h;
        block=block.next();
    }

    QPlainTextEdit::paintEvent(e);

}

void QDriverMessageTextEdit::contextMenuEvent(QContextMenuEvent *e)
{
    QMenu menu(this);

    QAction* ac;
    QList<QAction*> list;

    ac=new QAction(this);
    ac->setText(tr("Start/Stop"));
    connect(ac,SIGNAL(triggered()),this,SIGNAL(run()));
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    ac=new QAction(this);
    ac->setText("Select All");
    ac->setEnabled(this->toPlainText().size()>0);
    connect(ac,SIGNAL(triggered()),this,SLOT(selectAll()));
    list.append(ac);

    ac=new QAction(this);
    ac->setText("Copy");
    ac->setEnabled(this->textCursor().selectedText().size()>0);
    connect(ac,SIGNAL(triggered()),this,SLOT(copy()));
    list.append(ac);

    ac=new QAction(this);
    ac->setText("Clear");
    ac->setEnabled(this->toPlainText().size()>0);
    connect(ac,SIGNAL(triggered()),this,SLOT(clear()));
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    ac=new QAction(this);
    ac->setText("Save");
    ac->setEnabled(this->toPlainText().size()>0);
    connect(ac,SIGNAL(triggered()),this,SLOT(save()));
    list.append(ac);


    menu.addActions(list);

    menu.exec(e->globalPos());
}

void QDriverMessageTextEdit::save()
{
    QString str=QFileDialog::getSaveFileName(this,tr("Save"),QDir::currentPath(),
                                             tr("TXT File(*.txt)"));
    if(str!="")
    {
        QFile f(str);
        if(!f.open(QFile::ReadWrite))
        {
            return;
        }
        f.resize(0);
        QString str=toPlainText();
        str.replace("\n","\r\n");
        f.write(str.toLocal8Bit());
        f.close();
    }
}
